package com.hskn.hss.module.tldictlevel.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.core.utils.LoginUtils;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.tldictlevel.entity.TlEmergentDictLevel;
import com.hskn.hss.module.tldictlevel.mapper.TlEmergentDictLevelMapper;
import com.hskn.hss.module.tldictlevel.service.ITlEmergentDictLevelService;
import com.hskn.hss.module.tlresplan.entity.TlEmergentResplan;
import com.hskn.hss.module.tlresplan.mapper.TlEmergentResplanMapper;
import com.hskn.hss.module.userdepart.entity.SysUserDepart;
import com.hskn.hss.module.userdepart.mapper.SysUserDepartMapper;
import com.hskn.hss.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-18
 */
@Service
public class TlEmergentDictLevelServiceImpl extends ServiceImpl<TlEmergentDictLevelMapper, TlEmergentDictLevel> implements ITlEmergentDictLevelService {

    @Resource
    TlEmergentDictLevelMapper tlEmergentDictLevelMapper;
    @Resource
    SysUserDepartMapper sysUserDepartMapper;
    @Resource
    TlEmergentResplanMapper tlEmergentResplanMapper;

    @Override
    public AjaxResult insert(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        TlEmergentDictLevel tlEmergentDictLevel = params.getObject("data", TlEmergentDictLevel.class);

        QueryWrapper<SysUserDepart> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysUserDepart::getUserid, uid);
        SysUserDepart sysUserDepart = sysUserDepartMapper.selectOne(queryWrapper);
        if (null != sysUserDepart) {
            tlEmergentDictLevel.setDeptid(sysUserDepart.getDepid());
        }
        tlEmergentDictLevel.setCreateby(uid);
        tlEmergentDictLevel.setCreatetime(new Date());
        if (tlEmergentDictLevel.getStatus() == null || tlEmergentDictLevel.getStatus().isEmpty()) {
            tlEmergentDictLevel.setStatus("1");
        }
        tlEmergentDictLevelMapper.insert(tlEmergentDictLevel);
        return AjaxResult.success("操作成功");
    }

    @Override
    public AjaxResult update(JSONObject params) throws HssException {

        String uid = LoginUtils.getUser().getUid();
        TlEmergentDictLevel tlEmergentDictLevel = params.getObject("data", TlEmergentDictLevel.class);
        tlEmergentDictLevel.setUpdateby(uid);
        tlEmergentDictLevel.setUpdatetime(new Date());
        tlEmergentDictLevelMapper.updateById(tlEmergentDictLevel);
        return AjaxResult.success("操作成功");
    }

    @Override
    public AjaxResult deletebyid(JSONObject params) throws HssException {
        String id = params.getString("id");
        tlEmergentDictLevelMapper.deleteById(id);
        return AjaxResult.success("操作成功");
    }

    @Override
    public TlEmergentDictLevel getDetail(JSONObject params) throws HssException {
        String id = params.getString("id");
        TlEmergentDictLevel tlEmergentDictLevel = tlEmergentDictLevelMapper.selectById(id);
        return tlEmergentDictLevel;
    }

    @Override
    public List getSelectList() {
        List resultList = tlEmergentDictLevelMapper.getSelectList();
        return resultList;
    }

    @Override
    public JSONObject getList(JSONObject params) throws HssException {
        Long page = StringUtils.isEmpty(params.getLong("page")) ? 1 : params.getLong("page");
        Long size = StringUtils.isEmpty(params.getLong("size")) ? 10 : params.getLong("size");
        JSONObject json = new JSONObject();

        Map<String, Object> map = new HashMap();
        map.put("status", params.getString("status"));
        Page<TlEmergentDictLevel> listPage = new Page<>(page, size, true);
        IPage<TlEmergentDictLevel> iPage = tlEmergentDictLevelMapper.listTlEmergentDictLevel(listPage, map);
        json.put("total", iPage.getTotal());
        json.put("size", iPage.getSize());
        json.put("pages", iPage.getPages());
        json.put("current", iPage.getCurrent());
        List<TlEmergentDictLevel> tlEmergentDictLevelList = iPage.getRecords();
        for (TlEmergentDictLevel tlEmergentDictLevel : tlEmergentDictLevelList) {
            //根据id查询应预案
            List<TlEmergentResplan> tlEmergentResplanList = tlEmergentResplanMapper.findTlEmergentResPlanByEmergentLevelId(tlEmergentDictLevel.getId());
            tlEmergentDictLevel.setTlEmergentResplanList(tlEmergentResplanList);
        }
        json.put("resultlist", tlEmergentDictLevelList);
        return json;
    }
}
